﻿using System;
using System.Windows.Forms;

namespace BaiduMapTileCutter.GaoDe
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class StepPanel4_1 : UserControl
    {
        public StepPanel4_1()
        {
            InitializeComponent();
        }


        public void LoadPage()
        {
            string htmlString = "<!DOCTYPE html>"
                + "<html>"
                + "<head>"
                + "<meta charset=\"utf-8\">"
                + "<title>Get Point</title>"
                + "<script type=\"text/javascript\" src=\"http://api.map.baidu.com/api?v=1.2\"></script>"
                + "<style>"
                + "html, body {margin: 0;padding: 0;}"
                + "</style>"
                + "</head>"
                + "<body>"
                + "<div id=\"map\" style=\"width:492px;height:236px;background:#000;\"></div>"
                + "<script type=\"text/javascript\">"
                + "var map = new BMap.Map('map');"
                + "map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);"
                + "map.addEventListener('rightclick', function (e) {"
                + "    window.external.SetCenter(e.point.lng, e.point.lat);"
                + "});"
                + "</script>"
                + "</body>"
                + "</html>";
            // webviewMap.DocumentText = htmlString;
            webviewMap.ScriptErrorsSuppressed = true; //禁用错误脚本提示
                                                      //  webviewMap.Url = new Uri("https://www.baidu.com");
            String url = Application.StartupPath + @"\getpoint.html";
            webviewMap.Navigate(url);
            webviewMap.ObjectForScripting = this;

            WebviewMap_Resize(null, null);
        }

        public void SetCenter(string lng, string lat)
        {
            tbxLng.Text = lng;
            tbxLat.Text = lat;
        }


        public void getMapBounds(Decimal neLng, Decimal neLat, Decimal swLng, Decimal swLat)
        {
            NELng.Value = neLng;
            NELat.Value = neLat;

            SWLng.Value = swLng;
            SWLat.Value = swLat;
        }

        public LatLng getCenter()
        {
            float lat = Convert.ToSingle(tbxLat.Text);
            float lng = Convert.ToSingle(tbxLng.Text);
            return new LatLng(lat, lng);
        }

        private void WebviewMap_Resize(object sender, EventArgs e)
        {
            object[] objects = new object[2];
            objects[0] = webviewMap.Width - 20;
            objects[1] = webviewMap.Height - 25;
            webviewMap.Document.InvokeScript("resize", objects);
        }

        private void Button1_Click(object sender, EventArgs e)
        {



            Decimal tempLng = SWLng.Value + NELng.Value;
            Decimal tempLat = SWLat.Value + NELat.Value;
            tbxLng.Text = Convert.ToString(SWLng.Value);
            tbxLat.Text = Convert.ToString(NELat.Value);

            object[] objects = new object[2];
            objects[0] = tbxLng.Text;
            objects[1] = tbxLat.Text;

            if (checkBox1.Checked)
            {
                webviewMap.Document.InvokeScript("baiDuToGaoDe", objects);
            }
            else
            {
                webviewMap.Document.InvokeScript("setMarker", objects);
            }
            

            //设置可视范围

        }

        public void BaiDuToGaoDe(String lng, String lat)
        {
            tbxLng.Text = Convert.ToString(lng);
            tbxLat.Text = Convert.ToString(lat);
        }


        private void WebviewMap_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebviewMap_Resize(sender, e);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            object[] objects = new object[1];
            objects[0] = textBox2.Text;
            webviewMap.Document.InvokeScript("search", objects);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            webviewMap.Document.InvokeScript("getMapBounds", null);
        }
    }
}
